package com.topisv.tms.service.ediOrder;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Preconditions;
import com.topisv.tms.entity.ediOrder.VEdiOrder;
import com.topisv.tms.repository.ediOrder.VEdiOrderDao;
import com.topisv.tms.service.base.BaseService;

@Component
@Transactional(readOnly = true)
public class VEdiOrderService extends BaseService {
    
    private Logger log = LoggerFactory.getLogger(VEdiOrderService.class);
    
    @Autowired
    private VEdiOrderDao vEdiOrderDao;
    
    public long loadOrderCount(Map<String, Object> searchParams) {
        if (log.isDebugEnabled()) {
            log.debug("loadOrderCount param", searchParams);
        }
        Preconditions.checkArgument(!searchParams.isEmpty(), "searchParams can't be empty");
        Specification<VEdiOrder> spec = expWhereEdiOrderDto(searchParams);
        return vEdiOrderDao.count(spec);
    }
    
    public List<VEdiOrder> findAll(Map<String, Object> searchParams, PageRequest page) {
        Specification<VEdiOrder> spec = expWhereEdiOrderDto(searchParams);
        return vEdiOrderDao.findAll(spec,
            buildPageRequest(page.getPageNumber(), page.getPageSize(), page.getSort().toString())).getContent();
    }
    
    public List<VEdiOrder> findAll(Map<String, Object> searchParams) {
        Specification<VEdiOrder> spec = expWhereEdiOrderDto(searchParams);
        return vEdiOrderDao.findAll(spec);
    }
    
    /**
     * 导出操作查询条件封装
     * 
     * @param searchParams
     * @return
     */
    private Specification<VEdiOrder> expWhereEdiOrderDto(final Map<String, Object> searchParams) {
        
        return new Specification<VEdiOrder>() {
            @Override
            public Predicate toPredicate(Root<VEdiOrder> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
                Predicate pre = cb.conjunction();
                List<Expression<Boolean>> expression = pre.getExpressions();
                String customerCode = (String)searchParams.get("customerCode");
                if (StringUtils.isNotBlank(customerCode)) {
                    expression.add(cb.equal(r.get("customerCode").as(String.class),
                        customerCode.trim()));
                }
                String warehouseCode = (String)searchParams.get("warehouseCode");
                if (StringUtils.isNotBlank(warehouseCode)) {
                    expression.add(cb.equal(r.get("warehouseCode"), warehouseCode.trim()));
                }
                String status = (String)searchParams.get("status");
                if (StringUtils.isNotBlank(status)) {
                    expression.add(cb.equal(r.get("status"), status.trim()));
                }
                String orderStatus = (String)searchParams.get("orderStatus");
                if (StringUtils.isNotBlank(orderStatus)) {
                    expression.add(cb.equal(r.get("orderStatus"), orderStatus.trim()));
                }
                Date fromDate = (Date)searchParams.get("fromDate");
                if (fromDate != null) {
                    expression.add(cb.greaterThanOrEqualTo(r.get("workCrateTime").as(Date.class), fromDate));
                }
                Date toDate = (Date)searchParams.get("toDate");
                if (toDate != null) {
                    expression.add(cb.lessThanOrEqualTo(r.get("workCrateTime").as(Date.class), toDate));
                }
                String workOrderStatus = (String)searchParams.get("workOrderStatus");
                if (StringUtils.isNotBlank(workOrderStatus)) {
                    expression.add(cb.equal(r.get("workStatus").as(String.class), workOrderStatus.trim()));
                }
                return pre;
            }
        };
    }
    
}
